package com.jb.web.model.css.event;

import com.jb.web.application.SystemUtil;
import com.jb.web.bean.HtmlItem;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.bean.event.UIEvent;
import com.jb.web.context.WebContext;
import com.jb.web.form.BeanForm;
import com.jb.web.model.css.CssStyle;

import java.util.List;


public class EditStyleEH {
    public void onEditStyle(WebContext pCtx, UIEvent event) {
        String styleClassId = event.getParameter("styleClassId");

        if (styleClassId == null) {
            HtmlItem item = (HtmlItem) event.getSource();
            String styleToEdit = item.getStyleClass();

            if ((styleToEdit != null) && (styleToEdit.length() != 0)) {
                Object style = pCtx.getRespondTree().getDescendantByClientId(styleToEdit);
                pCtx.getSession().addAttribute("style2Edit", style);

                SystemUtil.showDialogBox(pCtx, "editStyle", "styleEditorPanel");
            } else {
                System.err.println("no styleclass to edit");
            }
        } else {
            Object style = pCtx.getRespondTree().getDescendantByClientId(styleClassId);
            pCtx.getSession().addAttribute("style2Edit", style);
        }
    }

    public void onInitialize(WebContext pCtx, UIEvent event) {
        CssStyle style = (CssStyle) pCtx.getSession().getAttribute("style2Edit");

        if ((style == null) || !style.getPage().equals(pCtx.getRespondTree())) {
            List list = pCtx.getStyleClasses();

            if (list.size() > 0) {
                pCtx.getSession().addAttribute("style2Edit", pCtx.getStyleClasses().get(0));
            }
        }
    }

    public void onSetDefaultStyle(WebContext pCtx, UIEvent event) {
        BeanForm form = (BeanForm) event.getSource();
        CssStyle cssStyle = (CssStyle) form.getModelBean();

        if (cssStyle != null) {
            cssStyle.restoreDefault();
        }
    }

    public void onShow(WebContext pCtx, UIEvent event) {
        UIComponentImpl html = (UIComponentImpl) event.getSource();

        /*
        if(!pCtx.getSession().isAuthenticated()
        ||
         !pCtx.getSession().getBlogger().getId().equals(pCtx.getSession().getUser().getId())
         )
         html.setVisible("false");*/
    }
}
